$NG.AllReady(function (page, { useDataIndexChange, useUpdateRow,useValuesChange , useUpdateRows }) {
    const mstform = $NG.getCmpApi("p_form_risk_track")
    const toolbar = $NG.getCmpApi("toolbar_top");
	toolbar.getItem('imp').setProps({children:'引用风险问题清单'})
    const user = $NG.getUser()
    $NG.updateState((updater)=>{
        updater.data.p_form_risk_track.setProps({
            u_feedback_dt:formatDate(new Date(), "yyyy-mm-dd")
        })
    })
    
    const useArr = [
        "p_form_risk_track.u_clf",
        "p_form_risk_track.u_gs",
        "p_form_risk_track.u_prc",
        "p_form_risk_track.u_cailiao",
    ]
    useArr.forEach(item=>{
        useValuesChange(({args})=>{
            $NG.updateState((updater)=>{
                updater.data.p_form_risk_track.setProps({
                    u_total:(mstform.getItem("u_clf").getValue() || 0)
                    +((mstform.getItem("u_gs").getValue()||0)*(mstform.getItem("u_prc").getValue()||0))
                    +(mstform.getItem("u_cailiao").getValue()||0)
                })
            })
        },item)
    })
useValuesChange(({args})=>{
    console.log(`日期监听触发`, args[0]);
    isDate(mstform.getItem("u_phid_source").getValue(), mstform.getItem("u_feedback_dt").getValue())
},"p_form_risk_track.u_feedback_dt")


useValuesChange(({args})=>{
    console.log(`状态监听触发`, args[0]);
    isDate(mstform.getItem("u_phid_source").getValue(), mstform.getItem("u_feedback_dt").getValue())
},"p_form_risk_track.u_sfgb")
    // 监听项目
    let phid_pc = null
    setInterval(()=>{
        if(phid_pc !== mstform.getItem("phid_pc").getValue()){
            getProjectInfo()
        }
        phid_pc = mstform.getItem("phid_pc").getValue()
    },1000)
    // 获取项目信息
     // 获取项目
     getProjectInfo()
     function getProjectInfo(isReturn = false, phid = mstform.getItem("phid_pc").getValue(), fun){
        if(!mstform.getItem("phid_pc").getValue()) return 
        try{
            setTimeout(()=>{
                $NG.execServer("getPtInfoByPhid",{phid: phid}, (res)=>{
                    const _data = JSON.parse(res.data)[0].extendObjects
                    console.log(`获取项目信息`, _data);
                    if(isReturn){
                        fun(_data)
                    } else {
                        $NG.updateState((updater)=>{
                            updater.data.p_form_risk_track.setProps({
                                u_pc_no: _data.pc_no, //项目编码
                                u_devision: {
                                    //事业部or组织
                                    value: _data.org_phid,
                                    label: _data.org_name,
                                },
                                phid_emp:{
                                    label:_data.project_manager_name,
                                    value:_data.project_manager_phid
                                },
                                u_sqgw: {
                                    //售前顾问
                                    value: _data.user_sqgw_phid,
                                    label: _data.user_sqgw_name,
                                },
                                u_regional_center: {
                                    //区域中心
                                    value: _data.regional_center_phid,
                                    label: _data.regional_center_name,
                                },
                                u_product_line: {
                                    //产品线
                                    value: _data.midwife_phid,
                                    label: _data.midwife_name,
                                },//行业线
                                u_industry_line: {
                                    value: _data.hyx_phid,
                                    label: _data.hyx_name,
                                  },
                                u_dept: {
                                    //部门
                                    value: _data.dept_phid,
                                    label: _data.dept_name,
                                },
                                u_pc_type: {//项目类型
                                    value: _data.type_phid,
                                    label: _data.type_name,
                                },
                            })
                        })
                    }
                })
            },100)
        }catch(err){
            $NG.alert("获取项目信息发生失败请检查~")
        }
    }
    // 判断日期是否过期模块
    function  isDate(phid,gzDate){
        if(!phid||!gzDate||!mstform.getItem("u_sfgb").getValue()) return 
            console.log(`phid`,this.phid);
            $NG.execServer("getFxqdInfo1", {phid}, (res)=>{
                // 获取分线清单
                console.log(`获取风险清单信息`, res);
                const _data = JSON.parse(res.data)[0].extendObjects
                console.log(_data);
                const date = _data.u_jhjj_dt
                const off = mstform.getItem("u_sfgb").getValue()
                let temp = null
                $NG.updateState((updater)=>{
                    if(isDateTime(gzDate,date)&&Number(off)==0){
                            temp = "03"
                        } else if(!isDateTime(gzDate,date)&&Number(off)==0){
                            temp = "04"
                        } else if(isDateTime(gzDate,date)&&Number(off)==1){
                            temp = "02"
                        } else if(!isDateTime(gzDate,date)&&Number(off)==1){
                            temp = "01"
                        }
                        console.log(`temp::::`,temp);
                        updater.data.p_form_risk_track.setProps({
                            u_stat:temp
                        })
                })
            })
    }

    // 判断日期
    function isDateTime(date1,date2){
        const _date1 = new Date(date1).getTime()
        const _date2 = new Date(date2).getTime()
        const end = _date2-_date1
        if(end>=0){
            return true
        } else {
            return false
        }
    }

    // 格式化日期
function formatDate(dateStr, outputFormat) {
    const date = new Date(dateStr);

    if (isNaN(date)) {
        return "无效的日期字符串";
    }

    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');

    let formattedDate;

    switch (outputFormat) {
        case "yyyy-mm-dd":
            formattedDate = `${year}-${month}-${day}`;
            break;
        case "yyyy-mm-dd HH:mm":
            formattedDate = `${year}-${month}-${day} ${hours}:${minutes}`;
            break;
        // 可以根据需要继续添加其他格式的处理逻辑
        default:
            formattedDate = "不支持的输出格式";
    }

    return formattedDate;
}
});

